{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "This notebook show how to use beta version ToolAgent."
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "7804b4b4e92eef16"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001B[31;1m\u001B[1;3mAgent Start...\u001B[0m\n",
      "\u001B[36;1m\u001B[1;3m[User] What is the weather in Beijing tomorrow? What is the square root of the temperature?\u001B[0m\n",
      "\u001B[33;1m\u001B[1;3m[Thought] I need to search for the weather forecast in Beijing for tomorrow to find out the temperature, and then I can use the temperature to calculate the square root.\n",
      "I have not yet gathered any actual weather data for the calculation, so it’s premature to consider the calculation step.\u001B[0m\n",
      "\u001B[33;1m\u001B[1;3m[Action] ddg_websearch args: {'query': 'Beijing weather forecast tomorrow'}\u001B[0m\n",
      "\u001B[33;1m\u001B[1;3m[Observation] -6 °C Mo -5 °C Tu -2 °C We 2 °C Th 3 °C Fr 2 °C Sa 2 °C Su 0 °C Meteogram Temperature (°C) Precipitation (mm) Gust (km/h) New snow (cm) -11 -12 -11 -7 -5 -5 -6 -4 -3 -3 -3 -5 -5 -6 -5 -2 2 3 2 2 0 1 -1 0 -1 0 Webcams Olympic Park Zone 8 km view.iap.ac.cn:8080 Air quality (AQI) Today 59 °F 40 °F 4 mph - 9 h 1 hour view The weather forecast has very high predictability. Compare different forecasts with MultiModel. Weather report for Beijing During the night and in the afternoon clear skies prevail. Before noon a few clouds are expected. It is a sunny day. Temperatures peaking at 59 °F. Beijing Weather Tomorrow - Accurate Forecast for Beijing Today (Beijing) World China Beijing Beijing Beijing Weather Forecast Now 09:28, 05 Dec +32 °F RealFeel +28°F Pressure 29.8 inHg Humidity 60% Wind N, 4.5 mph Wind gust 6.7 mph Cloudiness 0% Visibility 10 mi. More Sunrise: 07:19 Sunset: 16:49 Daylight: 09:30 Moon Phase: Third quarter China Beijing Beijing Weather Forecast. Providing a local hourly Beijing weather forecast of rain, sun, wind, humidity and temperature. The Long-range 12 day forecast also includes detail for Beijing weather today. Live weather reports from Beijing weather stations and weather warnings that include risk of thunder, high UV index and forecast gales. Today 12 September +20°C +26°C Tomorrow 13 September +18°C +25°C Thursday 14 September +19°C +26°C Friday 15 September +20°C +27°C Week Night +20°C Feels like +21°C 0% 0 mm 3 mps 76% 755 mm Morning +20°C\u001B[0m\n",
      "\u001B[33;1m\u001B[1;3m[Thought] The search results show the temperature for tomorrow as -5 °C in Beijing. Now, I need to calculate the square root of this temperature. However, since the square root of a negative number is not a real number, I will assume the task refers to the square root of the absolute value of the temperature. I will now use the calculator tool to find the square root of 5.\n",
      "I correctly identified the need to calculate the square root of the temperature in Beijing for tomorrow, but I must ensure to handle the absolute value since the original temperature is negative.\u001B[0m\n",
      "\u001B[33;1m\u001B[1;3m[Action] calculator args: {'expression': 'sqrt(5)'}\u001B[0m\n",
      "\u001B[33;1m\u001B[1;3m[Observation] 2.23606797749979\u001B[0m\n",
      "\u001B[33;1m\u001B[1;3m[Thought] Now that I have calculated the square root of tomorrow's forecasted temperature in Beijing, I should use the finish tool to provide the final result.\n",
      "There is no self-criticism needed at this stage since the next step is clear and there are no outstanding issues or tasks.\u001B[0m\n",
      "\u001B[33;1m\u001B[1;3m[Action] finish args: {'result': 'The temperature in Beijing tomorrow is forecasted to be -5°C. The square root of the absolute value of this temperature is 2.23606797749979.'}\u001B[0m\n",
      "\u001B[32;1m\u001B[1;3m[Agent Result] The temperature in Beijing tomorrow is forecasted to be -5°C. The square root of the absolute value of this temperature is 2.23606797749979.\u001B[0m\n",
      "\u001B[38;5;200m\u001B[1;3mAgent End.\u001B[0m\n",
      "The temperature in Beijing tomorrow is forecasted to be -5°C. The square root of the absolute value of this temperature is 2.23606797749979.\n"
     ]
    }
   ],
   "source": [
    "from promptulate.beta.agents.tool_agent.agent import ToolAgent\n",
    "import promptulate as pne\n",
    "\n",
    "tool_list = [pne.tools.ddg_websearch, pne.tools.calculator]\n",
    "agent = ToolAgent(tools=tool_list, llm=pne.ChatOpenAI(model=\"gpt-4-1106-preview\"))\n",
    "resp: str = agent.run(\"What is the weather in Beijing tomorrow? What is the square root of the temperature?\")\n",
    "print(resp)"
   ],
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-01-22T14:43:46.711562700Z",
     "start_time": "2024-01-22T14:43:00.772953800Z"
    }
   },
   "id": "initial_id",
   "execution_count": 1
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
